package com.i7time.qutan.controller;

import java.util.HashMap;
import java.util.Map;


import com.i7time.entity.User;
import com.i7time.qutan.utils.IMUtil;
import com.i7time.qutan.utils.ReturnMap;
import com.i7time.qutan.utils.TokenManager;
import com.i7time.service.UserService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;



@Controller
@RequestMapping("/user")
public class UserController {

	Logger logger=Logger.getLogger(UserController.class);

	@Autowired
	public UserService userService;

//	UserService userService=new UserServiceImpl();
	/*
	 * 验证用户是否存在
	 */
	@ResponseBody
	@RequestMapping("isExist")
	public Map isExist(@RequestBody Map map){
		try{
			HashMap result=ReturnMap.getSuccessMap();

			String tel=(String)map.get("tel");
			if(null==tel){
				return ReturnMap.exceptionMap;
			}

			if(userService.isExist(tel)){
				result.put("isExist","1");
			}else{
				result.put("isExist","0");
			}
			return result;
		}catch(Exception e){
			logger.error("",e);
			return ReturnMap.exceptionMap;
		}
	}
	
	/*
	 * 注册
	 */
	@ResponseBody
	@RequestMapping("register")
	public Map register(@RequestBody Map map){
		try{
			String tel=(String)map.get("tel");
			String password=(String)map.get("pwd");
			String sex=(String)map.get("sex");
			String birthday=(String )map.get("birthday");
			String avatar=(String)map.get("avatar");
			String nickname=(String)map.get("nickname");

			User user=userService.register(tel,password,nickname,avatar,sex,birthday);
			IMUtil.addUser(user.getId()+"");

			HashMap result=ReturnMap.getSuccessMap();

			HashMap usermap=new HashMap<String,String>();
		usermap.put("userid",user.getId()+"");
		usermap.put("tel",user.getTel());
		usermap.put("name",user.getUsername());
		usermap.put("nickName",user.getNick_name());
		usermap.put("sex",user.getSex());
		usermap.put("avatar",user.getAvatar());
		usermap.put("birthday",user.getBirthday());
		usermap.put("token",TokenManager.makeToken(user.getId()+""));
		usermap.put("tokenExpireTime",TokenManager.setExpire(user.getId()+""));
		usermap.put("isBusiness",user.getIsBusiness());
		usermap.put("isThirdLogin",user.getIsThirdLogin());
		usermap.put("thirdLoginId",user.getThirdLoginId());
		usermap.put("isRegister","1");

		result.put("registerResult",usermap);
			return result;
		}catch(Exception e){
			logger.error("",e);
			return ReturnMap.exceptionMap;
		}
	}
	
	
	/*
	 * 登录
	 */
	@ResponseBody
	@RequestMapping("login")
	public Map login(@RequestBody Map map){
		try{
        HashMap result;
        String tel=(String)map.get("tel");
        String pwd=(String)map.get("pwd");

		User user=userService.getUserByTel(tel);
		if(null==user || null==pwd){
			return ReturnMap.PassFailMap;
		}

		if(!pwd.equals(user.getPassword())){
			return ReturnMap.PassFailMap;
		}

		String token= TokenManager.getToken(user.getId()+"");
		if("".equals(token)) {
			token = TokenManager.makeToken(user.getId() + "");
		}




            result=ReturnMap.getSuccessMap();
			HashMap usermap=new HashMap<String,String>();
			usermap.put("userid",user.getId()+"");
			usermap.put("tel",user.getTel());
			usermap.put("name",user.getUsername());
			usermap.put("nickName",user.getNick_name());
			usermap.put("sex",user.getSex());
			usermap.put("avatar",user.getAvatar());
			usermap.put("birthday",user.getBirthday());
			usermap.put("token",token);
			usermap.put("tokenExpireTime",TokenManager.setExpire(user.getId()+""));
			usermap.put("isBusiness",user.getIsBusiness());
			usermap.put("isThirdLogin",user.getIsThirdLogin());
			usermap.put("thirdLoginId",user.getThirdLoginId());

			result.put("user",usermap);


            return result;
		}catch(Exception e){
			logger.error("",e);
			return ReturnMap.exceptionMap;
		}

        }





	/*
	 * 第三方登录
	 */
	@ResponseBody
	@RequestMapping("thirdLogin")
	public Map thirdLogin(@RequestBody Map map){

		try{
		HashMap result=new HashMap<String,Object>();
		String openid=(String)map.get("openid");
		String thirdLoginType=(String)map.get("thirdLoginType");
		String nickname=(String)map.get("nickname");
		String sex=(String)map.get("sex");
		String avatar=(String)map.get("avatar");

		/*第一次第三方登录的用户需要网IM服务添加用户*/
			User findeuser=userService.getUserByOpenid(openid);




		User user=userService.thirdLogin(openid,thirdLoginType,nickname,sex,avatar);
			//如果第三方登录之前没有该openid,则添加IM帐户
			 IMUtil.addUser(user.getId()+"");

		HashMap usermap=ReturnMap.getSuccessMap();
		usermap.put("userid",user.getId()+"");
		usermap.put("tel",user.getTel());
		usermap.put("name",user.getUsername());
		usermap.put("nickName",user.getNick_name());
		usermap.put("sex",user.getSex());
		usermap.put("avatar",user.getAvatar());
		usermap.put("birthday",user.getBirthday());
		usermap.put("token",TokenManager.makeToken(user.getId()+""));
		usermap.put("tokenExpireTime",TokenManager.setExpire(user.getId()+""));
		usermap.put("isBusiness",user.getIsBusiness());
		usermap.put("isThirdLogin",user.getIsThirdLogin());
		usermap.put("thirdLoginId",user.getThirdLoginId());

		result.put("user",usermap);



		return result;
		}catch(Exception e){
			logger.error("",e);
			return ReturnMap.exceptionMap;
		}
	}


	/*
	 *重置密码
	 */
	@ResponseBody
	@RequestMapping("resetPwd")
	public Map resetPwd(@RequestBody Map map){
		try{
		Long userid=(Long)map.get("userid");
		String oldPwd=(String)map.get("oldPwd");
		String newPwd=(String)map.get("newPwd");
			if(null==userid || null==newPwd ||null==oldPwd){
				return ReturnMap.exceptionMap;
			}

		User user=userService.getUserById(userid);
		//如果密码不正确
		if(!oldPwd.equals(user.getPassword())){
			return ReturnMap.PassFailMap;
		}

		userService.resetPassword(userid,newPwd);


		HashMap result=ReturnMap.getSuccessMap();

		result.put("changeState","1");


		return result;
	}catch(Exception e){
		logger.error("",e);
		return ReturnMap.exceptionMap;
	}

	}




	/*
	 *忘记密码
	 */
	@ResponseBody
	@RequestMapping("forgetPwd")
	public Map forgetPwd(@RequestBody Map map){
		try {
			String tel = (String) map.get("tel");
			String newPwd = (String) map.get("newPwd");
			User user = userService.getUserByTel(tel);
			if(null==tel || null==newPwd){
				return ReturnMap.exceptionMap;
			}
			userService.resetPassword(user.getId(), newPwd);

			HashMap result = ReturnMap.getSuccessMap();
			result.put("changeState", "1");


			return result;
		}catch(Exception e){
			logger.error("",e);
			return ReturnMap.exceptionMap;
		}


	}

	/*
	 *test
	 */
	@ResponseBody
	@RequestMapping("test")
	public Map test(@RequestBody Map map){


		HashMap result=new HashMap<String,Object>();
		result.put("test","test OK!");



		return result;

	}
		




		

}
